Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yarn workspaces + nested pulumi project + local package reference = "Could not include required dependency" #2661

Closed
brandonbloom opened this issue Apr 22, 2019 · 22 comments · Fixed by #15426
Assignees
Labels
area/tooling kind/bug Some behavior is incorrect or out of spec language/javascript resolution/fixed This issue was fixed

Comments

@brandonbloom
Copy link

Here's a gutted project structure that matches what I'm trying to get working:

https://github.com/brandonbloom/pulumi-workspaces-repro

In short, there are three packages: "core", "local", and "cloud". The bulk of the code is implemented in "core" and local development primarily operates via ts-node-dev running "local/index.ts", which imports "core". Then, there is a "cloud" package, which hosts the Pulumi bits, which also reuses core. Yarn creates a single top-level node_modules directory, and attempting to import core produces this error: Could not include required dependency 'example-core'

As a workaround, I've added an index.ts file that does require('./packages/cloud') in the root and moved the Pulumi *.yaml files to the root as well. Now pulumi up works if you omit -C packages/cloud, but I'd like to keep the Pulumi files contained to their package.

@brandonbloom
Copy link
Author

In addition to the index.ts/files-in-root workarounds, if I add a dependency they are not getting deployed in to the lambda. To workaround that, I have to use cloud-aws:functionIncludePackages: handy-redis or similar.

@arthur-caillaud
Copy link

arthur-caillaud commented Oct 21, 2019

Is there any support for yarn workspaces ? I think I am facing the same issue on a big monorepo managed using yarn workspaces. Some dependencies seem to not be found by pulumi cli but they are actually located in the node_modules found in the root directory.

Could not include required dependency '@outmind/lambdas' in '/.../outmind-app/aws-architecture'.
Could not include required dependency '@pulumi/pulumi' in '/.../outmind-app/aws-architecture'.
Could not include required dependency '@pulumi/aws' in '/.../outmind-app/aws-architecture'.
Could not include required dependency '@pulumi/awsx' in '/.../outmind-app/aws-architecture'.

All my packages are named @outmind/xxx and here the @outmind/lambdas package has not been found. Strangely, the lambda is successfully deployed but the generated code is pure gibberish.

Thank you for any support. Our company is really excited about this project but we can't manage to make it work in our environment.

@natew
Copy link

natew commented Dec 18, 2019

Following this as well, we've had in other solutions to really work hard to get around this. Having first
class support for this would be a huge win over many other platform.

@mstn
Copy link

mstn commented Dec 31, 2019

I am facing the same issue with lerna/yarn workspaces. The error message is also misleading imo: I do not think we want to include @pulumi/pulumi in the lambda bundle (and probably pulumi doesn't).

@JLarky
Copy link

JLarky commented Jan 29, 2020

I think you should just go to packages/cloud and run yarn add ... with all pulimi packages that you are going to use in that directory instead of just hoping that it will be requires from root

@lukehoban
Copy link
Member

Looked into this a little today.

It appears the issue is that we use https://github.com/npm/read-package-tree to read the local package.json and recursively walk dependencies and their package.jsons to find the transitive dependencies of a package. However, read-pacakge-tree does not handle Yarn Workspaces. It assumes that wherever the package.json file is, the node_modules next to it will contain all of the child modules that are direct dependencies of that package.json.

https://github.com/npm/read-package-tree/blob/master/rpt.js#L109

We will likely either need to maintain a fork of read-package-tree that handles this case, or abandon read-package-tree and find a way to construct these dependencies more directly.

@ringods
Copy link
Member

ringods commented Apr 10, 2020

Related: #4349

@aecorredor
Copy link

Facing the same issue here in a yarn workspaces + lerna monorepo. I have a common package where I have shared code. I'm getting the same Could not include required dependency '@project/common' error. Is there plans to add support for this?

@JakeGinnivan
Copy link

FWIW this is my solution. I have created a pulumi.js node script which runs tsc -b and also added dynamoDB locking.

https://gist.github.com/JakeGinnivan/a8a9487b427755f267ea6bf7ec42699f

@JakeGinnivan
Copy link

I just make sure I have a single pulumi project for my mono repo and use webpack to bundle lambdas and code which pulumi uploads

@vvo
Copy link

vvo commented Jul 15, 2020

I had success by adding into my pulumi files package.json:

  "workspaces": {
    "nohoist": [
      "@pulumi/aws"
    ]
  },

See here: https://classic.yarnpkg.com/blog/2018/02/15/nohoist/
This basically forces yarn to put some packages locally instead of at the root of the project

@ringods
Copy link
Member

ringods commented Jul 16, 2020

@vvo this only works for Yarn v1. This option is gone for Yarn v2.

@austinrivas
Copy link

Has there been any movement on this issue?

As it stands it does not seem like pulumi works with yarn workspaces.

@DanNeish
Copy link

@JakeGinnivan do you have a working example of how you do that?

@JakeGinnivan
Copy link

@DanNeish I've now switched my approach and am building out an NX plugin to make it easy to use Pulumi in an nx.dev mono repo.

I am now trying to create a small pulumi program for each service in the mono repo, then working towards being able to nx affected:up to deploy the appropriate stacks which have changes using NX's impact analysis.

@lukehoban lukehoban added kind/bug Some behavior is incorrect or out of spec language/javascript labels Jul 12, 2021
@liamawhite
Copy link
Contributor

liamawhite commented Oct 10, 2021

So yarn recently added the nmHoistingLimits option, this ensures that the workspace has a local node_modules.

By adding the following to the package.json in the workspace that is deploying the inline lamdas you can enable this per-workspace:

  "installConfig": {
    "hoistingLimits": "workspaces"
  }

Note: you may have to clear your yarn cache.

The problem I'm facing now is that the way Yarn does this for all local workspaces is to add a symlink.

ls -lart node_modules/@tetrateio-components
total 0
lrwxr-xr-x   1 liamwhite   42 Oct 10 11:12 aws-lambda -> ../../../../../../../components/aws/lambda
...

When Pulumi tries to bundle these it gets confused and thinks this is a file. So throws the following error:

Diagnostics:
  pulumi:pulumi:Stack (test-organization.test.managementplane.api.azure-alpha):
    error: Error: failed to register new resource azure-alpha-delete-tenants [aws:lambda/function:Function]: 2 UNKNOWN: failed to compute asset hash: asset path 'node_modules/@tetrateio-components/aws-aurora' is a directory; try using an archive
        at Object.registerResource (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/node_modules/@pulumi/aws/node_modules/@pulumi/pulumi/runtime/resource.js:219:27)
        at new Resource (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/node_modules/@pulumi/aws/node_modules/@pulumi/pulumi/resource.js:217:24)
        at new CustomResource (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/node_modules/@pulumi/aws/node_modules/@pulumi/pulumi/resource.js:309:9)
        at new Function (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/node_modules/@pulumi/lambda/function.ts:467:9)
        at new CallbackFunction (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/node_modules/@pulumi/lambda/lambdaMixins.ts:362:9)
        at new CronLambda (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/components/aws/lambda/cron.ts:26:28)
        at Object.<anonymous> (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/projects/organization/test/managementplane/api/index.ts:61:1)
        at Module._compile (node:internal/modules/cjs/loader:1092:14)
        at Module.m._compile (/Users/liamwhite/src/github.com/tetrateio/tetrate/cloud/projects/organization/test/managementplane/api/node_modules/ts-node/src/index.ts:439:23)
        at Module._extensions..js (node:internal/modules/cjs/loader:1121:10)

So I think if we can get the asset library to handle symlinks, this should just work?

p.s. I'm happy to do this if someone points me in the right direction.

Related, #2980.

@pmuller
Copy link

pmuller commented Oct 12, 2021

I use Yarn workspaces to manage a monorepo containing dozen of pulumi projects and a few library packages.

The key to make this work in my experience is to:

  1. Tell Yarn to avoid hoisting your dependencies using nohoist (feel free to improve glob matching for your use case):
{
[...]
  "workspaces": {
    "packages": [
      "lib/*",
      "projects/*"
    ],
    "nohoist": [
      "@yourscope/*/**"
    ]
  }
}
  1. Do not declare your monorepo internal dependencies in your package.json (you don't want yarn to mess with it), but use TypeScript project references - Pulumi >3.x works fine with it, even to deploy Lambda functions!

That said, this is a workaround. Making Pulumi work well with Yarn's package hoisting would be awesome!

@liamawhite
Copy link
Contributor

liamawhite commented Oct 12, 2021

For Yarn >=v2 you can do this ^^ in the .yarnrc.yml file by adding the following:

nodeLinker: node-modules
nmHoistingLimits: workspaces

Unfortunately, this doesn't work for us as a yarn install now takes 15 minutes (and counting) on the link step. Which, as we use the Pulumi operator means this is not an option for us.

@liamawhite
Copy link
Contributor

liamawhite commented Oct 13, 2021

For everyone using yarn 2+ workspaces once #8215 gets into a release, you can do the package.json configuration in my above comment and it should all just work.

lukehoban pushed a commit that referenced this issue Oct 25, 2021
See #2661 for context.

Bascially, yarn workspaces rely on symlinks for monorepo-local packages.

I think this also resolves #2980

Signed-off-by: Liam White <liam@tetrate.io>
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 2, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. When executing the function, I get "The security token included in
   the request is invalid." So something still isn't configured right.
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 10, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. When executing the function, I get "The security token included in
   the request is invalid." So something still isn't configured right.
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 11, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. When executing the function, I get "The security token included in
   the request is invalid." So something still isn't configured right.
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 11, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. When executing the function, I get "The security token included in
   the request is invalid." So something still isn't configured right.
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 27, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. When executing the function, I get "The security token included in
   the request is invalid." So something still isn't configured right.
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Aug 31, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. Vitest fails with the error below. I've filed a bug report with
   Pulumi (# 10480)

Error: Could not find property info for real property on object: sdk
 ❯ node_modules/@pulumi/runtime/closure/createClosure.ts:1103:27
 ❯ fulfilled node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:18:58
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Sep 15, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. Vitest fails with the error below. I've filed a bug report with
   Pulumi (# 10480)

Error: Could not find property info for real property on object: sdk
 ❯ node_modules/@pulumi/runtime/closure/createClosure.ts:1103:27
 ❯ fulfilled node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:18:58
dave-burke added a commit to skill-collectors/guesstimator that referenced this issue Sep 15, 2022
Two issues:
1. Pulumi can't bundle dependencies from node_modules in the project
   root. See pulumi/pulumi#2661 Creating a
   symlink from node_modules -> infra/node_modules works, but that's
   pretty hokey.
2. Vitest fails with the error below. I've filed a bug report with
   Pulumi (# 10480)

Error: Could not find property info for real property on object: sdk
 ❯ node_modules/@pulumi/runtime/closure/createClosure.ts:1103:27
 ❯ fulfilled node_modules/@pulumi/pulumi/runtime/closure/createClosure.js:18:58
@devdoshi
Copy link

is computeCodePaths relevant to this problem?

@bennettp123
Copy link

I stumbled onto this issue trying to get mixins working with pnpm, nx and esbuild

Here's what didn't work for me:

  • node-linker=hoisted—deployed fine, but it didn't bundle produced a runtime error (Runtime.ImportModuleError: Error: Cannot find module 'tslib')
  • CallbackFunctionArgs.codePathOptions['extraIncludePackages']—deployed fine, but didn't actually include the tslib package
            codePathOptions: {
                extraExcludePackages: ['tslib'],
            },
    image

Here's what does work for me:

  • node-linker=hoisted in .npmrc

  • force tslib to be included in the bundle, using CallbackFunctionArgs.codePathOptions:

    (new aws.cloudwatch.EventRule(/* ... */)).onEvent(
        'hello',
        new aws.lambda.CallbackFunction('hello', {
            runtime: aws.lambda.Runtime.NodeJS18dX,
            codePathOptions: {
                extraIncludePaths: ['../../node_modules/tslib'],
            },
            callback: async (event, context) => {
                console.log('hello from aws lambda!')
            },
        }
    ), {})

@bennettp123
Copy link

bennettp123 commented Jan 20, 2023

       codePathOptions: {
           extraExcludePackages: ['tslib'],
       },

omg I just saw the typo! No surprise tslib was missing from the bundle 🤦🏻

After fixing the typo, it still doesn't work—but at least I get an error message 😂

            codePathOptions: {
                extraIncludePackages: ['tslib'],
            },

image

github-merge-queue bot pushed a commit that referenced this issue Feb 22, 2024
# Description

When we compute the code paths for function serialisation, we are not
handling yarn/npm workspace setups where the node_modules might be in a
different directory.

Fixes #15436
Fixes #2661
Fixes #7168
Fixes #3013

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
@pulumi-bot pulumi-bot added the resolution/fixed This issue was fixed label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/tooling kind/bug Some behavior is incorrect or out of spec language/javascript resolution/fixed This issue was fixed
Projects
Status: backlog
Development

Successfully merging a pull request may close this issue.